<?php
/**
 * demo1.php
 * 变量引用与复制
 * @author  zen <gtcfla@gmail.com>
 * @date    2018/2/2
 */
$a = range(0, 3);
xdebug_debug_zval('a'); //refcount=0[个数], is_ref=0[是否引用]
//echo memory_get_usage() . "\n";

// COW Copy On Write 复制机制
$b = &$a;
xdebug_debug_zval('a');
//echo memory_get_usage() . "\n";
$a = range(0, 3);
xdebug_debug_zval('a');
//echo memory_get_usage() . "\n";

/**
a: (refcount=1, is_ref=0)=array (0 => (refcount=0, is_ref=0)=0, 1 => (refcount=0, is_ref=0)=1, 2 => (refcount=0, is_ref=0)=2, 3 => (refcount=0, is_ref=0
)=3)
a: (refcount=2, is_ref=1)=array (0 => (refcount=0, is_ref=0)=0, 1 => (refcount=0, is_ref=0)=1, 2 => (refcount=0, is_ref=0)=2, 3 => (refcount=0, is_ref=0
)=3)
a: (refcount=2, is_ref=1)=array (0 => (refcount=0, is_ref=0)=0, 1 => (refcount=0, is_ref=0)=1, 2 => (refcount=0, is_ref=0)=2, 3 => (refcount=0, is_ref=0
)=3)
 */